﻿Ext.define('Admin.view.orderapprove.OrderApproveViewController', {
    extend: Ext.app.ViewController,
    alias: 'controller.orderapprove',
    onClickOrderApproveClaimButton: function(view, recIndex, cellIndex, item, e, record) {
		var grid = view.up('gridpanel');//获取Grid视图
		var selModel = grid.getSelectionModel();//获取Grid的SelectionModel
		if (selModel.hasSelection()) {//判断是否选中记录
			if ((selModel.getSelection()[1]) != null){
				Ext.Msg.alert('提示',"请选择其中一行数据进行编辑!");
			}else{
				var record = selModel.getSelection()[0];//获取选中的第一条记录
				Ext.Ajax.request({
					method: 'post',
					defaultHeaders: {
						"X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
					},
					url: 'order/claim/' + record.get('taskId'),
					method: 'post',
					params : { 
						orderId:record.data.orderId
					},
					success: function(response, options) {
						var json = Ext.util.JSON.decode(response.responseText);
						if (json.success) {
							Ext.Msg.alert('操作成功', json.msg, function() {
								grid.getStore().reload();
							});
						} else {
							Ext.Msg.alert('操作失败', json.msg);
						}
					}
				});
			}
		}else{
        	Ext.Msg.alert('提示',"请选择一行数据进行编辑!");
        }
    },
    	
    	
	onClickOrderApproveCompleteWindowButton: function(btn,view, recIndex, cellIndex, item, e, record) {
			var grid = btn.up('gridpanel');//获取Grid视图
			var selModel = grid.getSelectionModel();//获取Grid的SelectionModel
			if (selModel.hasSelection()) {//判断是否选中记录
				if ((selModel.getSelection()[1]) != null){
					Ext.Msg.alert('提示',"请选择其中一行数据进行编辑!");
				}else{
					var record = selModel.getSelection()[0];//获取选中的第一条记录
					var taskDefinitionKey = record.get('taskDefinitionKey');
					if (taskDefinitionKey == 'deptManagerAudit') {
						this.setCurrentView('deptManagerAuditForm', '部门经理审批');
					}else if (taskDefinitionKey == 'administrativeAssistantAudit') {
						this.setCurrentView('administrativeAssistantAuditForm','行政助理审批');
					}else if (taskDefinitionKey == 'hrAdministrativeAssistantAudit') {
						this.setCurrentView('hrAdministrativeAssistantAuditForm','人事行政经理审批');
					}else if (taskDefinitionKey == 'modifyApply') {
						this.onClickOrderAdjustButton(btn);
					}
				}
			}else{
				Ext.Msg.alert('提示',"请选择一行数据进行编辑!");
			}
		},
    setCurrentView: function(view, title) {
        if (view) {
            var cfg = Ext.apply({
                    xtype: 'orderapprove-window',
                    items: [
                        Ext.apply({
                            xtype: view
                        }, '')
                    ]
                }, {
                    title: title
                });
            Ext.create(cfg);
        }
    },
	complete: function(url, variables ,grid ,form,orderId,pass,refuseReason){
		// 转换JSON为字符串
	    var keys = "", values = "", types = "";
		if (variables) {
			Ext.each(variables, function (item) {
				if (keys != "") {
					keys += ",";
					values += ",";
					types += ",";
				}
				keys += item.key;
				values += item.value;
				types += item.type;
            });
		}
		Ext.Ajax.request({
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            url: url,
            params : { 
			 	keys: keys,
		        values: values,
		        types: types,
				orderId:orderId,
				pass: pass,
                refuseReason: refuseReason
			}, 
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('操作成功', json.msg, function() {
                    	form.up('window').close();
                        grid.getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('操作失败', json.msg);
					form.up('window').close();
                    grid.getStore().reload();
                }
            }
        });
	},
    	
    onClickDeptManagerAuditFormSubmitButton: function(btn) {
        var gridPanel = Ext.getCmp('orderapprove-grid');
	    	var form = btn.up('form');
	    	var record = gridPanel.getSelectionModel().getSelection()[0];
	    	var url = 'order/complete/' + record.get('taskId');
	    	var pass = form.getForm().findField('deptManagerApproved').getGroupValue();
			var refuseReason = '';
			if (pass == true) {
				var variables = [{
					key: 'deptManagerApproved',
					value: true,
					type: 'B'
				}, {
					key: 'administrativeAssistant',
					value: 'HR_STAFF',
					type: 'S'
				}];
			} else {
				var variables = [{
					key: 'deptManagerApproved',
					value: false,
					type: 'B'
				}, {
					key: 'reapplyUserId',
					value: record.data.userName,
					type: 'S'
				}];
				refuseReason = form.getForm().findField('backReason').getValue();
			}
	        this.complete(url,variables,gridPanel,form,record.data.orderId,pass,refuseReason);   
    },
	onClickAdministrativeAssistantAuditFormSubmitButton: function(btn) {
        var gridPanel = Ext.getCmp('orderapprove-grid');
	    	var form = btn.up('form');
	    	var record = gridPanel.getSelectionModel().getSelection()[0];
	    	var url = 'order/complete/' + record.get('taskId');
	    	var pass = form.getForm().findField('administrativeAssistantApproved').getGroupValue();
			var refuseReason = '';
			if (pass == true) {
				var variables = [{
					key: 'administrativeAssistantApproved',
					value: true,
					type: 'B'
				}, {
					key: 'hrManager',
					value: 'HR_MANAGER',
					type: 'S'
				}];
			} else {
				var variables = [{
					key: 'administrativeAssistantApproved',
					value: false,
					type: 'B'
				}, {
					key: 'reapplyUserId',
					value: record.data.userName,
					type: 'S'
				}];
				refuseReason = form.getForm().findField('backReason').getValue();
			}
	        this.complete(url,variables,gridPanel,form,record.data.orderId,pass,refuseReason);   
    },
	onClickHrAdministrativeAssistantAuditFormSubmitButton: function(btn) {
        var gridPanel = Ext.getCmp('orderapprove-grid');
	    	var form = btn.up('form');
	    	var record = gridPanel.getSelectionModel().getSelection()[0];//.getLastSelected();
	    	var url = 'order/complete/' + record.get('taskId');
	    	var pass = form.getForm().findField('hrAdministrativeAssistantApproved').getGroupValue();
			var refuseReason = '';
			if (pass == true) {
				var variables = [{
					key: 'hrAdministrativeAssistantApproved',
					value: true,
					type: 'B'
				}];
			} else {
				var variables = [{
					key: 'hrAdministrativeAssistantApproved',
					value: false,
					type: 'B'
				}, {
					key: 'reapplyUserId',
					value: record.data.userName,
					type: 'S'
				}];
				refuseReason = form.getForm().findField('backReason').getValue();
			}
	        this.complete(url,variables,gridPanel,form,record.data.orderId,pass,refuseReason);   
    },
	onClickOrderAdjustButton: function(btn) {	
		var grid = Ext.getCmp('orderapprove-grid');
        var record = grid.getSelectionModel().getSelection()[0];
	    //创建修改window和form
	    var orderGridWindow = Ext.create('order-update-window',{
			title:'调整申请表单',
			items: [{xtype: 'order-adjust-form'}]
		});
			//让form加载选中记录
	    orderGridWindow.down("form").getForm().loadRecord(record);
    },
	onClickAdjustFormSubmitButton: function (btn) {
    	var form = btn.up('form');
		form.getForm().submit({
            params: {
                "_csrf": document.getElementsByTagName('meta')['_csrf'].content
            },
            failure: function(form, action) {
                Ext.Msg.alert('错误', response.status + ' ' + response.statusText);
            },       
			url:'order/update',
			success: function(form, action){    
				Ext.Msg.alert('Success', action.result.msg,function(){
					btn.up('window').close();
					if(Ext.getCmp('orderGrid') != null){
						Ext.getCmp('orderGrid').getStore().reload();
					}
					Ext.getCmp('orderapprove-grid').getStore().reload();
				});       
			},       
			failure: function(form, action){
				Ext.Msg.alert('Error', action.result.msg);
			}
		});
    },
	onClickOrderReapplyButton:function(btn) {
		var grid = btn.up('gridpanel');//获取Grid视图
		var selModel = grid.getSelectionModel();//获取Grid的SelectionModel
		if (selModel.hasSelection()) {//判断是否选中记录
			if ((selModel.getSelection()[1]) != null){
				Ext.Msg.alert('提示',"请选择其中一行数据进行编辑!");
			}else{
				var record = selModel.getSelection()[0];//获取选中的第一条记录
				var url = 'order/complete/' + record.get('taskId');
				var reapplyUserId = record.get('assignee');
				var variables = [{
					key: 'reApply',
					value: true,
					type: 'B'
				}];
				var keys = '',
					values = '',
					types = '';
				if (variables) {
					Ext.each(variables, function (item) {
						if (keys != '') {
							keys += ',';
							values += ',';
							types += ',';
						}
						keys += item.key;
						values += item.value;
						types += item.type;
					});
				}
				Ext.Ajax.request({
					defaultHeaders: {
						"X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
					},
					url: url,
					method: 'post',
					params: {
						keys: keys,
						values: values,
						types: types,
						orderId: record.data.orderId,
						pass: true,
						reapplyUserId:reapplyUserId
					},
					success: function (response, options) {
						var json = Ext.util.JSON.decode(response.responseText);
						if (json.success) {
							Ext.Msg.alert('操作成功', json.msg);
							grid.getStore().reload();
						}if(Ext.getCmp('orderGrid') != null){
							Ext.getCmp('orderGrid').getStore().reload();
						}
						Ext.getCmp('orderapprove-grid').getStore().reload();
					},
					failure: function(form, action){
						Ext.Msg.alert('Error', action.result.msg);
					}
				});
			}
		}else{
		Ext.Msg.alert('提示',"请选择一行数据进行编辑!");
		}		
    },
	onClickOrderCancelButton: function(btn) {
		var grid = btn.up('gridpanel');//获取Grid视图
		var selModel = grid.getSelectionModel();//获取Grid的SelectionModel
		if (selModel.hasSelection()) {//判断是否选中记录
			if ((selModel.getSelection()[1]) != null){
				Ext.Msg.alert('提示',"请选择其中一行数据进行编辑!");
			}else{
				var record = selModel.getSelection()[0];//获取选中的第一条记录
				var url = 'order/complete/' + record.get('taskId');
				var reapplyUserId = record.get('assignee');
				var variables = [{
					key: 'reApply',
					value: false,
					type: 'B'
				}];
				var keys = '',
					values = '',
					types = '';
				if (variables) {
					Ext.each(variables, function (item) {
						if (keys != '') {
							keys += ',';
							values += ',';
							types += ',';
						}
						keys += item.key;
						values += item.value;
						types += item.type;
					});
				}
				Ext.Ajax.request({
					defaultHeaders: {
						"X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
					},
					url: url,
					method: 'post',
					params: {
						keys: keys,
						values: values,
						types: types,
						orderId: record.data.orderId,
						pass: false,
						reapplyUserId:reapplyUserId
					},
					success: function (response, options) {
						var json = Ext.util.JSON.decode(response.responseText);
						if (json.success) {
							Ext.Msg.alert('操作成功', json.msg);
							grid.getStore().reload();
						}if(Ext.getCmp('orderGrid') != null){
							Ext.getCmp('orderGrid').getStore().reload();
						}
						Ext.getCmp('orderapprove-grid').getStore().reload();
					},
					failure: function(form, action){
						Ext.Msg.alert('Error', action.result.msg);
					}
				});
			}
		}else{
		Ext.Msg.alert('提示',"请选择一行数据进行编辑!");
		}		
    },
	onClickBackReasonButton:function(btn) {		
		var grid = btn.up('gridpanel');//获取Grid视图
		var selModel = grid.getSelectionModel();//获取Grid的SelectionModel
		if (selModel.hasSelection()) {//判断是否选中记录
			if ((selModel.getSelection()[1]) != null){
				Ext.Msg.alert('提示',"请选择其中一行数据进行编辑!");
			}else{
			    var record = selModel.getSelection()[0];//获取选中的第一条记录
			    //创建修改window和form
			    var orderGridWindow = Ext.create(Ext.apply({xtype:'backReasonWindow'}));
				//让form加载选中记录
			    orderGridWindow.down("form").getForm().loadRecord(record);
			}
		}else{
        	Ext.Msg.alert('提示',"请选择一行数据进行编辑!");
        }
	}
});